//
//  LoginViewController.m
//  ApeExample
//
//  Created by Eton on 2017/7/27.
//  Copyright © 2017年 王-庭协. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginPresenter.h"
#import "AppRequest+Auth.h"

@interface LoginViewController ()
@property (nonatomic, strong) LoginPresenter * presenter;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *appNameLabel;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *userCodeButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *txtViewTopLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoBgViewHeightLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonBgViewHeightLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *trdLoginBgViewHeightLayout;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupData];
    
    [self setupView];
    
    [self setupPresenter];

}
#pragma mark - setup

- (void)setupData{
    self.phoneTextField.text = @"13163906015";
    self.passwordTextField.text = @"123456";
}

- (void)setupView{
    [self.loginButton tx_setBackgroundColor:RGBS(204) forState:UIControlStateDisabled];
}

- (void)setupPresenter{
    self.presenter = [[LoginPresenter alloc] init];
    
    // 给模型的属性绑定信号
    // 只要账号文本框一改变，就会给account赋值
    RAC(self.presenter, account) = _phoneTextField.rac_textSignal;
    RAC(self.presenter, pwd) = _passwordTextField.rac_textSignal;
    
    // 绑定登录按钮
    RAC(self.loginButton,enabled) = self.presenter.enableLoginSignal;
    
    RAC(self,isLoading) = [self.presenter.loginCommand executing];
}

#pragma mark - Action

- (IBAction)loginAction:(id)sender {
    [[self.presenter.loginCommand execute:[RACTuple tupleWithObjects:_phoneTextField.text,_passwordTextField.text, nil]] subscribeNext:^(id x) {
        NSLog(@"%@",x);
    } error:^(NSError *error) {
        [self loadingFailure:error];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
